package com.qing.web_freemarke.util;

import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;

public class PageResultInfo<T> {

	private long total;
	private List<T> list;
	private int pageNum;
	private int pageSize;
	private int pages;
	private int[] navigatepageNums;

	public PageResultInfo() {
	}

	public PageResultInfo(PageInfo pageInfo) {
		this.total = pageInfo.getTotal();
		this.list = pageInfo.getList();
		this.pageNum = pageInfo.getPageNum();
		this.pageSize = pageInfo.getPageSize();
		this.pages = pageInfo.getPages();
		this.navigatepageNums = pageInfo.getNavigatepageNums();
	}

	public List<T> getListPaging(List<T> list, int pageNum, int pageSize) {
		//开始下标
		int startIndex = (pageNum - 1) * pageSize;
		//结束下标 subList()方法不包含结束下标的元素
		int endIndex = pageNum * pageSize;
		//list总条数
		int total = list.size();
		//总页数
		int pageCount = 0;
		//通过取余决定是否给总页数加1
		int num = total % pageSize;
		if (num == 0) {
			pageCount = total / pageSize;
		} else {
			pageCount = total / pageSize + 1;
		}
		//如果当前页是最后一页的话 要包含集合的最后一条数据，因为sublist方法本身结束的下标是不包含当前元素的
		if (pageNum == pageCount) {
			endIndex = total;
		}
		if (total < pageSize) {
			return list.subList(0, total);
		}
		if (total == 0) {
			return new ArrayList<>();
		}
		return list.subList(startIndex, endIndex);
	}

	public long getTotal() {
		return total;
	}

	public void setTotal(long total) {
		this.total = total;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPages() {
		return pages;
	}

	public void setPages(int pages) {
		this.pages = pages;
	}

	public int[] getNavigatepageNums() {
		return navigatepageNums;
	}

	public void setNavigatepageNums(int[] navigatepageNums) {
		this.navigatepageNums = navigatepageNums;
	}
}
